package com.han.demo.controller;

import com.han.demo.entity.User;
import com.han.demo.repository.CommonRepository;
import com.han.demo.repository.UserRepository;
import com.han.demo.service.PersonService;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping(value = "/user")
public class UserController {


    @Resource
    private PersonService personService;
    @Autowired
    private CommonRepository commonRepository;
    @Autowired
    private UserRepository userRepository;


    @PostMapping(value = "/add")
    @ResponseBody
    public User addUser(@RequestBody User user){
        User useri = new User();
        useri.setId(1);
        useri.setName("hnajiael");
        useri.setAge(18);
        useri.setRemark("测试");
        System.out.println("结果：" + useri.toString());
        List<User> list = new LinkedList<>();
        personService.batchSave(list);
        return useri;
    }

    @GetMapping(value = "/saveBatch")
    @ResponseBody
    public User batchUser(){
        User useri = new User();
        useri.setId(1);
        useri.setName("hnajiael");
        useri.setAge(18);
        useri.setRemark("测试");
        System.out.println("结果：" + useri.toString());
        List<User> list = new LinkedList<>();
        personService.batchSave(list);
        return useri;
    }

    @GetMapping(value = "/jpa")
    @ResponseBody
    public User testJpa(){
        User user = userRepository.findById(1);
        System.out.println("设置属性前：" + user.toString());
        user.setName("张馨雨3");
        System.out.println("设置属性后：" + user.toString());

        String sql = "select * from user u where u.id = :id";
        Map<String,Object> params = new HashMap<>(16);
        params.put("id",1);
        List<Map<String, Object>> mapList = commonRepository.commonQuery(sql, params);
        System.out.println("结果是："  + mapList.toString());
        return user;
    }

    public static void main(String[] args) {
        int[] nums = {4,1,2,1,2};
        int i = singleNumber(nums);
        System.out.println("结果："+ i);
        System.out.println("异或结果：4^1^2^1^2=" + (4^1^2^1^2));
    }
    public static int singleNumber(int[] nums) {
        Arrays.sort(nums);
        int single = 0;
        if(nums[0] != nums[1]){
            single = nums[0];
        }
        if(nums[nums.length-2] != nums[nums.length -1]){
            single = nums[nums.length - 1];
        }
        for(int i = 1;i < nums.length - 1;i ++){
            if(nums[i] != nums[i - 1] && nums[i] != nums[i + 1]){
                single = nums[i];
            }
        }
        return single;
    }

}
